/* EasyWay Game Engine
 * Copyright (C) 2006 Daniele Paggi.
 *  
 * Written by: 2006 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * 
 */
package org.easyway.editor.forms.components.extended;

import java.util.ArrayList;

import org.easyway.editor.commands.Command;
import org.easyway.editor.commands.user.GetPoint;
import org.easyway.editor.forms.components.Button;
import org.easyway.editor.forms.components.InputText;
import org.easyway.geometry2D.VectorSpeed;
import org.easyway.input.Mouse;
import org.easyway.utils.MathUtils;
import org.lwjgl.opengl.GL11;

/**
 * @author Proprietario
 * 
 */
public class GetRotationButton extends Button {

	private static final long serialVersionUID = 1922164668432745243L;

	public static ArrayList<GetRotationButton> list = new ArrayList<GetRotationButton>(
		10);

	public static GetRotationButton findById(int id) {
		for (GetRotationButton pb : list) {
			if (pb.id == id) {
				return pb;
			}
		}
		return null;
	}

	int id;

	public GetRotationButton(int id) {
		super("getRotation", "select");
		this.id = id;
		list.add(this);
	}

	InputText inputRot;

	@Override public void command() {
		final float xy[] = GetPoint.getPoint();
		if (xy == null)
			return;
		AngleDrawer c = new AngleDrawer();
		c.xy = xy;
		float xy2[] = GetPoint.getPoint();
		c.toDestroy = true;
		if (xy2 == null) {
			return;
		}
		System.out.println("trying1");
		float vec[] = new float[] { xy2[0] - xy[0], xy2[1] - xy[1] };
		if (inputRot != null) {
			System.out.println("trying2");
			inputRot.setValue(""
					+ MathUtils.radToDeg((float) VectorSpeed.getAngle(vec[0],
						vec[1])));
		}
	}

	class AngleDrawer extends Command {
		public boolean toDestroy = false;

		public float xy[];

		public void creation() {
			toDestroy = false;
		}

		public void loop() {
			if (toDestroy)
				destroy();
		}

		public void render() {
			GL11.glDisable(GL11.GL_TEXTURE_2D);
			GL11.glColor4f(1, 0.5f, 0.5f, 1.0f);
			GL11.glBegin(GL11.GL_LINES);
			GL11.glVertex2f(xy[0], xy[1]);
			GL11.glVertex2f(Mouse.getX(), Mouse.getY());
			GL11.glEnd();
			GL11.glEnable(GL11.GL_TEXTURE_2D);
		}

	}

}
